package tw.edu.ttu.cse.FourCLab;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FileOperation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.file_operation);
    Button openFileA = (Button)findViewById(R.id.FileA);
    openFileA.setOnClickListener(openFileAClickListener);
    Button openFileB = (Button)findViewById(R.id.FileB);
    openFileB.setOnClickListener(openFileBClickListener);
    Button openFileSD = (Button)findViewById(R.id.FileSD);
    openFileSD.setOnClickListener(openFileSDClickListener);
    Button saveFileA = (Button)findViewById(R.id.SaveA);
    saveFileA.setOnClickListener(saveFileAClickListener);
    Button saveFileB = (Button)findViewById(R.id.SaveB);
    saveFileB.setOnClickListener(saveFileBClickListener);
    Button saveFileSD = (Button)findViewById(R.id.SaveSD);
    saveFileSD.setOnClickListener(saveFileSDClickListener);
    }
    
    private OnClickListener openFileAClickListener = new OnClickListener() {
        public void onClick(View v){
        	EditText fileContent = (EditText)findViewById(R.id.FileContent);
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		FileInputStream fileA = openFileInput("FileA");
        		String tempString = new String();
            	byte[] buffer = new byte[1024];
            	int length = -1;
            	while((length = fileA.read(buffer)) != -1) {                 
            		tempString = tempString + (new String(buffer)).subSequence(0, length);  
            	}
            	fileContent.setText(tempString);
            	fileA.close();
        	}
        	catch(Exception e){
        		fileContent.setText("No such file");
        	}
        	currentFile.setText("Current File : File A");        	
        }
    };
    
    private OnClickListener saveFileAClickListener = new OnClickListener() {
        public void onClick(View v){
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		FileOutputStream fileA = openFileOutput("FileA",0);
        		String fileContent = ((EditText)findViewById(R.id.FileContent)).getText().toString();
            	byte[] buffer = fileContent.getBytes();
            	int length = buffer.length;
            	fileA.write(buffer, 0, length);
            	fileA.close();
        	}
        	catch(Exception e){
        		currentFile.setText("Error");
        	}
        }
    };
    
    private OnClickListener openFileBClickListener = new OnClickListener() {
        public void onClick(View v){
        	EditText fileContent = (EditText)findViewById(R.id.FileContent);
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		FileInputStream fileB = openFileInput("FileB");
        		String tempString = new String();
            	byte[] buffer = new byte[1024];
            	int length = -1;
            	while((length = fileB.read(buffer)) != -1) {                 
            		tempString = tempString + (new String(buffer)).subSequence(0, length);          
            	}
            	fileContent.setText(tempString.toString());
            	fileB.close();
        	}
        	catch(Exception e){
        		fileContent.setText("No such file");
        	}
        	currentFile.setText("Current File : File B");
        	
        }
    };
    
    private OnClickListener saveFileBClickListener = new OnClickListener() {
        public void onClick(View v){
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		FileOutputStream fileB = openFileOutput("FileB",0);
        		String fileContent = ((EditText)findViewById(R.id.FileContent)).getText().toString();
            	byte[] buffer = fileContent.getBytes();
            	int length = buffer.length;
            	fileB.write(buffer, 0, length);
            	fileB.close();
        	}
        	catch(Exception e){
        		currentFile.setText("Error");
        	}
        }
    };
    
    private OnClickListener openFileSDClickListener = new OnClickListener() {
        public void onClick(View v){
        	EditText fileContent = (EditText)findViewById(R.id.FileContent);
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		FileInputStream fileSD = new FileInputStream("/sdcard/FileSD");
        		String tempString = new String();
            	byte[] buffer = new byte[1024];
            	int length = -1;
            	while((length = fileSD.read(buffer)) != -1) {                 
            		tempString = tempString + (new String(buffer)).subSequence(0, length);          
            	}
            	fileContent.setText(tempString.toString());
            	fileSD.close();
        	}
        	catch(Exception e){
        		fileContent.setText("No such file");
        	}
        	currentFile.setText("Current File : File SD");
        	
        }
    };
    
    private OnClickListener saveFileSDClickListener = new OnClickListener() {
        public void onClick(View v){
        	TextView currentFile = (TextView)findViewById(R.id.TextView01);
        	try{
        		File file = new File("/sdcard","FileSD");
        		file.createNewFile();
        		FileOutputStream fileSD = new FileOutputStream(file);
        		String fileContent = ((EditText)findViewById(R.id.FileContent)).getText().toString();
            	byte[] buffer = fileContent.getBytes();
            	int length = buffer.length;
            	fileSD.write(buffer, 0, length);
            	fileSD.close();
        	}
        	catch(Exception e){
        		e.printStackTrace();
	        	currentFile.setText("Error");
        	}
        }
    };

}
